package cn.caishen.report.dao.system;

import cn.caishen.report.domain.system.Module;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 模块管理持久层
 * @author LB
 * @description LB
 * @create 2021-03-22 14:25:56
 **/
@Mapper
public interface ModuleDao extends BaseMapper<Module> {

    /**
     * 根据登录用户查询模块信息
     * @param userId
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            " module.module_id, module.module_name, module.url, module.web_name, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor, module.order_by, module.request_url, module.is_report " +
            " FROM tb_module module " +
            " INNER JOIN tb_role_module roleModule ON module.module_id = roleModule.module_id " +
            " INNER JOIN tb_user_role userRole ON userRole.role_id = roleModule.role_id " +
            " WHERE userRole.user_id = #{userId} AND userRole.unit_id = #{unitId} AND COALESCE(module.delete_flag, 0) = 0 " +

            "<if test='isReport != null and isReport == 1'>" +
                " and module.is_report = 1 " +
            "</if>" +

            " GROUP BY " +
            " module.module_id, module.module_name, module.url, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor, module.order_by, module.request_url " +
            " ORDER BY module.order_by " +
            "</script>")
    List<Module> getModuleListByUserId(@Param("userId") Integer userId, @Param("unitId") Integer unitId, @Param("isReport") Integer isReport);

    /**
     * 该角色本身有的商户模块
     * @param roleId
     * @param unitId
     * @return
     */
    @Select("SELECT " +
            " module.module_id, module.module_name, module.url, module.web_name, module.parent_id, module.delete_flag, module.type, " +
            " module.level, module.create_time, module.creator, module.edit_time, module.editor, module.order_by, module.request_url, module.is_report " +
            " FROM tb_module module " +
            " INNER JOIN tb_role_module roleModule ON module.module_id = roleModule.module_id " +
            " WHERE roleModule.role_id = #{roleId} AND roleModule.unit_id = #{unitId} AND COALESCE(module.delete_flag, 0) = 0 " +
            " ORDER BY module.order_by ")
    List<Module> getModuleListByRoleId(Integer roleId, Integer unitId);
}